<div class="panel panel-primary">
    <div class="panel-heading">
        <div class="panel-title text-center">
            任务列表
        </div>
    </div>
    {{if eq (len .Body.data) 0}}
    <div class="panel-body text-center">
        任务列表为空
    </div>
    {{else}}
    <table  class="table table-bordered table-hover">
        <thead>
        <th class="text-center">服务名</th>
        <th class="text-center">运行状态</th>
        <th class="text-center">Hook状态</th>
        <th class="text-center">错误状态</th>
        <th class="text-center">操作</th>

        </thead>
        {{range .Body.data}}
        <tr>
            <td class="text-center">{{.Name}}</td>
            <td class="text-center">
                {{typeOfStatus .RunStatus}}
            </td>
            <td class="text-center">
                {{typeOfStatus .HookStatus}}
            </td>
            <td class="text-center">
                {{if gt (len .LastErrors) 0}}
                <a href="/errors/{{.Name}}"><img src="/statics/img/error.png" style="width: 20px;height: 20px"/></a>
                {{else}}
                <a href="#"><img src="/statics/img/ok.png" style="width: 20px;height: 20px"/></a>
                {{end}}
            </td>
            <td class="text-center">
                <div class="btn-group">
                    {{if ge (permlevel $.Body.user.Permissions) 3}}
                    <button type="button" class="btn btn-default" href="#" onclick="editJob('{{.Name}}')"><img src="/statics/img/edit.png" style="width: 20px;height: 20px"/></button>
                    <button type="button" class="btn btn-default" href="#" onclick="buildJob('{{.Name}}')"><img src="/statics/img/archiver.png" style="width: 20px;height: 20px"/></button>
                    <button type="button" class="btn btn-default" href="#" onclick="deleteJob('{{.Name}}')"><img src="/statics/img/delete.png" style="width: 20px;height: 20px"/></button>
                    {{end}}
                    {{if ge (permlevel $.Body.user.Permissions) 2}}
                    {{if eq .RunStatus 1}}
                    <button type="button" class="btn btn-default" onclick="stopJob('{{.Name}}')"><img src="/statics/img/stop.png" style="width: 20px;height: 20px"/></button>
                    {{else}}
                    <button type="button" class="btn btn-default" onclick="startJob('{{.Name}}')"><img src="/statics/img/start.png" style="width: 20px;height: 20px"/></button>
                    {{end}}
                    {{end}}
                    {{if ge (permlevel $.Body.user.Permissions) 1}}
                    <a class="btn btn-default" href="/logInfo/{{.Name}}/run" title="运行日志"><img src="/statics/img/run_logger.png"  style="width: 20px;height: 20px"/></a>
                    <a class="btn btn-default" href="/logInfo/{{.Name}}/hook" title="Hook日志"><img src="/statics/img/hook_logger.png" style="width: 20px;height: 20px"/></a>
                    {{end}}
                </div>
            </td>
        </tr>

        {{end}}
    </table>
    {{end}}

    <div class="panel-footer ">
        <a class="text-left" href="#" onclick="editJob('add')">add</a>
        <span style="margin-left: 100px" class="text-right">修改任务信息后，需要重新启动才能应用新的属性</span>
    </div>
</div>
<script type="application/javascript">
    function startJob(jobname) {
        layer.msg('确定启动任务‘'+jobname+'’？', {
            time: 0 //不自动关闭
            ,btn: ['确定', '取消']
            ,yes: function(index){
                $.ajax({
                    url: "/start/"+jobname,
                    data: {name:jobname},
                    type: 'POST',
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.msg)
                        if(data.code==0){
                            location.reload(true)
                        }
                    }
                })
            }
        });
    }
    function stopJob(jobname) {
        layer.msg('确定停止任务‘'+jobname+'’？', {
            time: 0 //不自动关闭
            ,btn: ['确定', '取消']
            ,yes: function(index){
                $.ajax({
                    url: "/stop/"+jobname,
                    data: {name:jobname},
                    type: 'POST',
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.msg)
                        if(data.code==0){
                            location.reload(true)
                        }
                    }
                })
            }
        });
    }
    function buildJob(jobname) {
        layer.msg('确定手动Hook任务‘'+jobname+'’？', {
            time: 0 //不自动关闭
            ,btn: ['确定', '取消']
            ,yes: function(index){
                $.ajax({
                    url: "/build/"+jobname,
                    data: {name:jobname},
                    type: 'POST',
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.msg)
                        if(data.code==0){
                            location.reload(true)
                        }
                    }
                })
            }
        });
    }
    function editJob(jobname) {
        var tts=jobname=='add'? "新建任务":"编辑任务‘"+jobname+"’的信息"
        layer.open({
            type: 2,
            area: ['600px', 'auto'],
            fixed: false, //不固定
            maxmin: true,
            title:tts,
            content: '/jobInfo/'+jobname
        });
    }
    function deleteJob(jobName){
        layer.msg('确定删除‘'+jobName+'’？', {
            time: 0 //不自动关闭
            ,btn: ['确定', '取消']
            ,yes: function(index){
                $.ajax({
                    url: "/deleteJob/"+jobName,
                    data: {name:jobName},
                    type: 'POST',
                    dataType: 'json',
                    success: function(data){
                        layer.msg(data.msg)
                        if(data.code==0){
                            location.reload(true)
                        }
                    }
                })
            }
        });

    }
</script>